package fm.controller;

import com.mongodb.DBObject;
import fm.cache.AreaCache;
import fm.dto.NeiyiUser;
import fm.entityEnum.CarouselScope;
import fm.exception.BizException;
import fm.mongo.MCondition;
import fm.mongo.MRel;
import fm.mongoService.UserService;
import fm.web.MediaTypes;
import fm.yichenet.mongo.service.ArticleService;
import fm.yichenet.mongo.service.CarouselService;
import fm.yichenet.mongo.service.GoodMgrService;
import org.apache.solr.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * Created by 61741 on 2017/12/1.
 */
@Controller
@RequestMapping("/home")
public class HomeController extends BaseController{
   @Autowired
   private CarouselService carouselService;
   @Autowired
   private UserService userService;
   @Autowired
   private GoodMgrService goodMgrService;
   @Autowired
   private ArticleService articleService;
    @RequestMapping("")
    public String index(ModelMap modelMap){
        modelMap.put("citys", AreaCache.getAllCity());
        modelMap.put("provinces",AreaCache.getAllProvince());
        return "portal/home";
    }

    /**
     * 获取首页轮播图
     * @param city
     * @param scope
     * @return
     */

    @ResponseBody
    @RequestMapping(value = "/slider/list/{scope}",method = RequestMethod.POST,produces = MediaTypes.JSON_UTF_8)
    public Map getsliderListData(String city,@PathVariable CarouselScope scope){
        LOGGER.info("getsliderListData: ctiy{} pagetype{}",city,scope);
        Map res = new HashMap();
        try{
            if(StringUtils.isEmpty(city)){
                throw new BizException("未能获取到城市编码，无法加载当前区域的广告图!");
            }
            List<DBObject> list = carouselService.getCarouselByScope(city,scope);
            res.put("data",list);
            this.success(res);
        }catch (BizException ex){
            LOGGER.error("发生错误:",ex);
            this.failed(res,ex.getMessage());
        }catch (Exception ex){
            LOGGER.error("发生异常",ex);
            this.failed(res,"服务器发生了未知的错误，请联系管理员或者开发者解决!");
        }
        return res;
    }

    /**
     * 首页全网搜索
     * @param type
     * @param keyword
     * @return
     */
    @ResponseBody
    @RequestMapping("/search")
    public  String getSearchinfo(String type,String keyword,ModelMap modelMap){
        LOGGER.info("getSearchinfo: type{} keyword{}",type,keyword);
        Map res = new HashMap();
        String repath =null;
        switch (type){
            case "shop":
                    modelMap.put("keyword",keyword);
                    repath= "portal/shopList";
                    this.success(res);
                break;
            case "good":
                modelMap.put("keyword",keyword);
                repath= "portal/goodsList/index";
                this.success(res);
                break;
            case "info":
                modelMap.put("keyword",keyword);
                repath= "portal/infoList";
                this.success(res);
                break;
                default:
                    this.failed(res,"请选择正确的搜索类型!");
                    repath= "portal/home";
        }
       return repath;
    }

}
